package com.animatedpicture.object;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import javax.swing.ImageIcon;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Bass implements IMovingObject{

	private static final double LARGE_CIRCLE_R = 27.0;
	private static final double SMALL_CIRCLE_R = 18.0;

	private static final double STICK_BODY_LENGTH = 40.0;
	private static final double STICK_BODY_WIDTH = 8.0;

	private static final double TOP_LENGTH = 12.0;
	private static final double TOP_WIDTH = 13.0;

	private static Boolean animateCheck = false;

	private Point objPosition;

	private Random mRand = new Random();

	private Color bodyColor;
	private Color stickColor;
	
	// Audio
	private AudioStream as;
	
	private Point handPosition;
	
	private int maxX;
	private int maxY;
	
	private int delX;
	private int delY;
	
	private String headType;
	private ImageIcon headImg;

	public Bass(String headType)
	{
		objPosition = null;
		handPosition = null;

		bodyColor = new Color(mRand.nextInt(256), mRand.nextInt(256), mRand.nextInt(256));
		stickColor = new Color(mRand.nextInt(256), mRand.nextInt(256), mRand.nextInt(256));
		
		// Get audio file from local storage
		try {
			File file = new File("./sound/Bass_Sound.wav");
			FileInputStream fis = new FileInputStream(file);
			as = new AudioStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.headType = headType;
		
		switch (this.headType) {
		case "Default":
			headImg = null;
			break;
		case "Yap Che Een":
			headImg = new ImageIcon("./Image/Yap.png");
			break;
		case "Kim Ik Soo":
			headImg = new ImageIcon("./Image/Kim.png");
			break;
		case "Wong Kok Sheng":
			headImg = new ImageIcon("./Image/Wong.png");
			break;
		case "Shin Yong Tae":
			headImg = new ImageIcon("./Image/Shin.png");
			break;
		case "Kim Seock Yoon":
			headImg = new ImageIcon("./Image/Kim_S.png");
			break;
		default:
			break;
		}
	}

	@Override
	public void draw(Graphics2D g2) {

		if (objPosition != null) {

			// DRAW MAN
			
			/*
			 * Man's body
			 */
			
			Graphics2D bodyG = g2;

			double bodyX = objPosition.x + LARGE_CIRCLE_R;
			double bodyY = objPosition.y - LARGE_CIRCLE_R*0.8;

			Rectangle2D body = new Rectangle2D.Double(bodyX, bodyY, 40, SMALL_CIRCLE_R*4);
			// Set Color b
			bodyG.setColor(Color.BLUE);
			bodyG.fill(body);

			/*
			 * Man's head
			 */
			
			Graphics2D headG = g2;

			double headX = bodyX + (15 - SMALL_CIRCLE_R);
			double headY = bodyY - SMALL_CIRCLE_R*2.5;

			Ellipse2D head = new Ellipse2D.Double(headX, headY, SMALL_CIRCLE_R*2.5, SMALL_CIRCLE_R*2.5);

			headG.setColor(Color.BLUE);
			
			Graphics g = (Graphics)g2;
			
			if (headImg == null) {
				headG.fill(head);
			}else {
				g.drawImage(headImg.getImage(), 
							(int)headX, 
							(int)headY, 
							(int)(SMALL_CIRCLE_R*2.5), 
							(int)(SMALL_CIRCLE_R*2.5), 
							null);
			}
			

			/*
			 * Man's left arm top
			 */
			
			Graphics2D leftArmTopG = g2;

			Line2D leftArmTop = new Line2D.Double(bodyX, 
					bodyY, 
					objPosition.x + LARGE_CIRCLE_R*0.3, 
					objPosition.y + LARGE_CIRCLE_R*0.3);

			leftArmTopG.setStroke(new BasicStroke(3));
			leftArmTopG.setColor(Color.BLUE);
			leftArmTopG.draw(leftArmTop);

			/*
			 * Man's left leg
			 */
			
			Graphics2D leftLegG = g2;

			Line2D leftLeg = new Line2D.Double(bodyX + (40/6), 
					bodyY + SMALL_CIRCLE_R * 4, 
					bodyX + (40/6), 
					bodyY + SMALL_CIRCLE_R * 7);
			
			leftLegG.setStroke(new BasicStroke(3));
			leftLegG.setColor(Color.BLUE);
			leftLegG.draw(leftLeg);

			/*
			 * Man's right leg
			 */
			
			Graphics2D rightLegG = g2;

			Line2D rightLeg = new Line2D.Double((bodyX + 40)-(40/6), 
					bodyY + SMALL_CIRCLE_R * 4, 
					(bodyX + 40)-(40/6), 
					bodyY + SMALL_CIRCLE_R * 7);
			
			rightLegG.setStroke(new BasicStroke(3));
			rightLegG.setColor(Color.BLUE);
			rightLegG.draw(rightLeg);

			// DRAW GUITAR

			/*
			 * Guitar Body - 1
			 */
			
			Graphics2D largeCircleG = g2;
			Ellipse2D largeCircle = new Ellipse2D.Double(objPosition.x, 
					objPosition.y, 
					LARGE_CIRCLE_R*2, 
					LARGE_CIRCLE_R*2);

			largeCircleG.setColor(bodyColor);
			largeCircleG.fill(largeCircle);

			/*
			 * Guitar Body - 2
			 */
			
			Graphics2D smallCircleG = g2;

			double smallCircleX = objPosition.x + LARGE_CIRCLE_R*1.5;
			double smallCircleY = objPosition.y + (LARGE_CIRCLE_R - SMALL_CIRCLE_R);

			Ellipse2D smallCircle = new Ellipse2D.Double(smallCircleX, 
					smallCircleY, 
					SMALL_CIRCLE_R*2, 
					SMALL_CIRCLE_R*2);

			smallCircleG.setColor(bodyColor);
			smallCircleG.fill(smallCircle);

			/*
			 * Guitar hole
			 */
			
			Graphics2D holeG = g2;

			double holeX = objPosition.x + LARGE_CIRCLE_R*0.5;
			double holeY = objPosition.y + LARGE_CIRCLE_R*0.5;

			Ellipse2D hole = new Ellipse2D.Double(holeX, holeY, LARGE_CIRCLE_R, LARGE_CIRCLE_R);

			holeG.setColor(Color.GRAY);
			holeG.fill(hole);

			/*
			 * Guitar Stick
			 */
			
			Graphics2D stickBodyG = g2;

			double stickBodyX = smallCircleX + SMALL_CIRCLE_R*2;
			double stickBodyY = smallCircleY + (SMALL_CIRCLE_R - STICK_BODY_WIDTH/2);

			Rectangle2D stickBody = new Rectangle2D.Double(stickBodyX, 
					stickBodyY, 
					STICK_BODY_LENGTH, 
					STICK_BODY_WIDTH);

			stickBodyG.setColor(stickColor);
			stickBodyG.fill(stickBody);

			/*
			 * Guitar Top
			 */
			
			Graphics2D topG = g2;

			double topX = stickBodyX + STICK_BODY_LENGTH;
			double topY = stickBodyY - (TOP_LENGTH - STICK_BODY_WIDTH)/2;

			Rectangle2D top = new Rectangle2D.Double(topX, topY, TOP_WIDTH, TOP_LENGTH);

			topG.setColor(Color.BLACK);
			topG.fill(top);

			// Guitar String

			for (int i = 0; i < 3; i++)
			{
				Graphics2D strG = g2;

				Line2D string = new Line2D.Double(holeX, 
						stickBodyY+(STICK_BODY_WIDTH/4)*(i+1), 
						topX+TOP_WIDTH, 
						stickBodyY+(STICK_BODY_WIDTH/4)*(i+1));

				strG.setStroke(new BasicStroke(1));
				strG.setColor(Color.DARK_GRAY);
				strG.draw(string);
			}

			// Right Arm

			Graphics2D rightArmG = g2;

			Line2D rightArm = new Line2D.Double(bodyX+40, 
					bodyY, 
					stickBodyX+STICK_BODY_LENGTH*0.8, 
					stickBodyY);

			rightArmG.setStroke(new BasicStroke(3));
			rightArmG.setColor(Color.BLUE);
			rightArmG.draw(rightArm);

			// Left Arm Low

			Graphics2D leftArmLowG = g2;

			Line2D leftArmLow = new Line2D.Double(objPosition.x + LARGE_CIRCLE_R*0.3, 
					objPosition.y + LARGE_CIRCLE_R*0.3, 
					handPosition.x, 
					handPosition.y);

			leftArmLowG.setStroke(new BasicStroke(3));
			leftArmLowG.setColor(Color.BLUE);
			leftArmLowG.draw(leftArmLow);

			if (animateCheck) {

				int minX = maxX - 10;
				int minY = maxY + 10;
				
				if (handPosition.x == maxX && handPosition.y == maxY) {
					
					delX = -5;
					delY = 5;
					
				}else if (handPosition.x == minX && handPosition.y == minY) {
					
					delX = 5;
					delY = -5;
					
				}
				
				handPosition.x += delX;
				handPosition.y += delY;
			}

		}

	}

	@Override
	public void setPosition(Point mPoint) {
		objPosition = mPoint;
		handPosition = new Point((int)(objPosition.x+LARGE_CIRCLE_R*1.5), (int)(objPosition.y+LARGE_CIRCLE_R/2+10));
		
		maxX = handPosition.x;
		maxY = handPosition.y;
		
		System.out.println("Guitar SetPosition : ("+mPoint.x+","+mPoint.y+")");
	}

	@Override
	public void animate(boolean trriger) {
		animateCheck = trriger;
	}

	@Override
	public void playMusic(boolean trriger) {
		if (trriger) {
			AudioPlayer.player.start(as);
		}else {
			AudioPlayer.player.stop(as);
		}
	}
}
